/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package org.myontology.specialpages;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.log4j.Logger;

/**
 * @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 *
 */
public abstract class SpecialPageRegistry {

	public SpecialPageRegistry() {
		init();
	}
	
	protected Logger logger = Logger.getLogger(getClass());
	
	private Map<String,SpecialPageConfig> pageMap = new ConcurrentHashMap<String, SpecialPageConfig>();
	private List<SpecialPageConfig> pageList = new CopyOnWriteArrayList<SpecialPageConfig>();
	
	public void reset() {
		pageMap = new ConcurrentHashMap<String, SpecialPageConfig>();
		pageList = new CopyOnWriteArrayList<SpecialPageConfig>();
	}
	
	/**
	 * <code>init</code> has to be overridden to register all special pages used in this registry
	 * all registered pages are shown in the Special pages list
	 * 
	 */
	public abstract void init();
	
	/**
	 * <code>registerPage</code> should be called from the init method. 
	 * the order the pages are registered in is the order the pages are displayed on screen
	 * avoid duplicate keys in your SpecialPageConfig. the second entry with the same key is omitted
	 * to track this, you can find an entry in your errorLog
	 * @param config
	 */
	protected void registerPage(SpecialPageConfig config) {
		logger.debug("registerPage: "+config.getKey()+" "+config.getLabel()+" "+config.getClass().getName());
		if (pageMap.containsKey(config.getKey())) {
			logger.error("duplicate Entry in PageRegistry: "+config.getKey());
		}
		else {
			pageMap.put(config.getKey(), config);
			pageList.add(config);
		}
	}
	
	/**
	 * returns all registered SpecialPageConfigs
	 * to display them use: getKey() and getLabel()
	 * @return
	 */
	public List<SpecialPageConfig> getSpecialPageConfigList() {
		return pageList;
	}
	
	/**
	 * creates and inits an instance of a special page
	 * the config for this page is retrieved with the given key
	 * @param key
	 * @return
	 */
	public SpecialPageHolder retrieveSpecialPage(String key) {
		checkKey(key);
		return retrieveSpecialPage(pageMap.get(key));
		
	}

	/**
	 * creates and inits an instance of a special page
	 * the config for this page 
	 * @param key
	 * @return
	 */
	
	public SpecialPageHolder retrieveSpecialPage(SpecialPageConfig c) {
		SpecialPageHolder sp = new SpecialPageHolder(c.cloneMe());
		return sp;
	}
	
	/**
	 * retrieves the label of a SpecialPageConfig registered in this registry
	 * @param key
	 * @return
	 */
	public String retrieveLabelForKey(String key) {
		checkKey(key);
		SpecialPageConfig conf = pageMap.get(key);
		return conf.getLabel();
	}
	
	private void checkKey(String key) {
		if (!pageMap.containsKey(key)) {
			logger.error("no SpecialPageConfig with key "+key+" could be found");
			throw new RuntimeException("no SpecialPageConfig found");
		}
	}
	
	
}
